#region Using

using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.Specialized;
using BlogEngine.Core;
using BlogEngine.Core.Providers.EloqueraProvider.Db;

#endregion

namespace BlogEngine.Core.Providers
{
    /// <summary>
    /// A storage provider for BlogEngine that uses XML files.
    /// <remarks>
    /// To build another provider, you can just copy and modify
    /// this one. Then add it to the web.config's BlogEngine section.
    /// </remarks>
    /// </summary>
    public partial class EloqueraBlogProvider : BlogProvider
    {

        /// <summary>
        /// The storage location is to allow Blog Providers to use alternative storage locations that app_data root directory.
        /// </summary>
        /// <returns></returns>
        public override string StorageLocation()
        {
           if(String.IsNullOrEmpty(System.Web.Configuration.WebConfigurationManager.AppSettings["StorageLocation"]))
               return @"~/app_data/";
            return System.Web.Configuration.WebConfigurationManager.AppSettings["StorageLocation"];

        }
        /// <summary>
        /// Loads the settings from the provider.
        /// </summary>
        public override StringDictionary LoadSettings() {
            EqSettings settings = (EqSettings)EqDb.ExecuteScalar("select EqSettings");
            if (settings == null)
                return null;
            StringDictionary dic = new StringDictionary();
            for (int i = 0; i < settings.Keys.Length; i++) {
                dic[settings.Keys[i]] = settings.Values[i];
            }
            return dic;
        }

        /// <summary>
        /// Saves the settings to the provider.
        /// </summary>
        public override void SaveSettings(StringDictionary settings)
        {
            if (settings == null)
                throw new ArgumentNullException("settings");
            List<string> keys = new List<string>();
            foreach (var key in settings.Keys) {
                keys.Add(key.ToString());
            }
            List<string> values = new List<string>();
            foreach (var val in settings.Values) {
                values.Add(val.ToString());
            }
            EqSettings eqSettings = (EqSettings)EqDb.ExecuteScalar("select EqSettings");
            if (settings == null)
                return;
            eqSettings.Keys = keys.ToArray();
            eqSettings.Values = values.ToArray();
            EqDb.Store(eqSettings);
        }
    }
}
